<?php
function list_sk(){

//PASANG JAVASCRIPT
	my_set_code_js('
	function confirmDelete(id){
		var t = confirm(\'Yakin akan menghapus data ?\');
		if(t){
			location.href=\'index.php?com=pr_sk_tunjangan&task=deletesk&id=\'+id;
		}
		return false;
	}
	');
	$header = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:5%'), 
		'SK No.'=>array('style'=>'border-bottom:2px solid;width:25%'),  
		'Tanggal Efektif'=>array('style'=>'text-align:left;border-bottom:2px solid;width:20%'),   
		'Komponen Gaji'=>array('style'=>'text-align:left;border-bottom:2px solid;width:35%'),   
		' '=>array('style'=>'border-bottom:2px solid;width:15%'), 
	);

	$query = "SELECT a.*,b.nama_komponen_gaji FROM pr_sk_tunjangan a 
		LEFT JOIN pr_komponen_gaji b ON a.komponen_gaji_id=b.komponen_gaji_id " ;
	
	$result = my_query($query);
	$i = 0;
	$row=array();
	while($ey = my_fetch_array($result)){
		$i++;
		$detailproperty = array(
			'href'=>'index.php?com=pr_sk_tunjangan&task=list_detail&sk_id='.$ey['sk_id'], 
			'title'=>'Detail'
		);
		$detail_button = button_icon( 'b_props.png' , $detailproperty  );
		$editproperty = array(
			'href'=>'index.php?com=pr_sk_tunjangan&task=edit_sk&id='.$ey['sk_id'],
			'rel'=>'facebox',
			'title'=>'Edit'
		);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );

		$deleteproperty = array(
			'href'=>'javascript:; ',
			'onclick'=>'javascript:confirmDelete('.$ey['sk_id'].');',
			'title'=>'Delete'
		);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );

		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'nomor'=>  $ey['nomor_sk'] ,
			'tgl_efektif'	=> position_text_align ( date("d-m-Y", strtotime($ey['tanggal'] ." 00:00:00" ) ), 'left'), 
			'tunjangan'	=> position_text_align ( $ey['nama_komponen_gaji'], 'left'), 
			'operasi'=> position_text_align(  $detail_button.' '.$edit_button.' '.$delete_button , 'right'),  
		);
		 
	}
	$datas = table_rows($row);  
	return   table_builder($header , $datas ,  5 , false  );
}

function form_sk( $id ){
	my_set_file_js(
		array(
			'components/system/jquery/combomulti/jquery.chainedSelects.js',
			'components/system/js/calendar/calendarDateInput.js' 
		)
	);

	$view = form_header( "..." , "..."  );
	$ftanggal= date('d-m-Y');
	$fields = my_get_data_by_id('pr_sk_tunjangan','sk_id',$id);
	if($fields){
		list($yyyytanggal , $mmtanggal,$ddtanggal ) = explode("-" ,$fields['tanggal'] );
		$ftanggal = $ddtanggal.'-'.$mmtanggal.'-'.$yyyytanggal;
	} 
	
	$nomor_sk = array(
			'name'=>'nomor_sk',
			'value'=>(isset($_POST['nomor_sk'])? $_POST['nomor_sk'] : $fields['nomor_sk']),
			'id'=>'nomor_sk',
			'type'=>'textfield',
			'size'=>'35'
		);
	$form_nomor = form_dynamic($nomor_sk);
	$view .= form_field_display( $form_nomor  , "Nomor SK"  );
	
	$tanggal = array(
			'name'=>'tanggal',
			'value'=>(isset($_POST['tanggal'])? $_POST['tanggal'] : $ftanggal),
			'id'=>'tanggal',
			'type'=>'textfield',
			'size'=>'45'
		);
	$form_tanggal = form_calendar($tanggal);
	$view .= form_field_display( $form_tanggal  , "Tanggal" );
	
	$gols = array();
	$query_golongan = "SELECT * FROM pr_komponen_gaji WHERE model_entry = 3 OR model_entry = 4";
	$re = my_query($query_golongan);
	 while($row = my_fetch_array($re) ){
		$gols[$row['komponen_gaji_id']] =$row['nama_komponen_gaji'];
	 }
	 $gol = array(
		'name'=>'komponen_gaji_id',
		'id'=>'komponen_gaji_id',
		'value'=>isset($_POST['komponen_gaji_id']) ? $_POST['komponen_gaji_id'] : $fields['komponen_gaji_id'] 
		);
	$form_field_gol = form_dropdown($gol , $gols);
	$view .= form_field_display( $form_field_gol   , "Komponen Gaji");

	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
}
function form_sk_validate($id){
	$errsubmit = false;
	$err = array();
	 
	$nomor_sk =  trim($_POST["nomor_sk"]); 
	if( $nomor_sk == ''){
		$errsubmit = true;
		$err[] = "Nomor SK belum di isi";
	}

	$code =  trim($_POST["komponen_gaji_id"]); 
	if( $code == '0'){
		$errsubmit = true;
		$err[] = "Komponen gaji belum di isi";
	}
	list( $ddtanggal, $mmtanggal,$yyyytanggal ) = explode("-" ,$_POST['tanggal'] );
	$ftanggal = $yyyytanggal.'-'.$mmtanggal.'-'.$ddtanggal;
	$query="SELECT sk_id FROM pr_sk_tunjangan 
		WHERE tanggal= '".$ftanggal.
		"' AND komponen_gaji_id=".$code.
		" AND sk_id<>".$id ; 
	if (my_num_rows( my_query($query)) >0){
		$errsubmit = true;
		$err[] = "Komponen sudah ada pada tanggal yang sama";	
	}	
	
	if( $errsubmit){
		return $err;
	}
	return $errsubmit;
}


function submit_rapel($sk_id , $id){
	$datas = array( 
		'komponen_gaji_id'=>my_type_data_int($_POST['komponen_gaji_id']),
		'tanggal'=>my_type_data_str(form_date_converter($_POST['tanggal'])),
		'tanggal_mulai'=>my_type_data_str(form_date_converter($_POST['tanggal_mulai'])),
		'tanggal_selesai'=>my_type_data_str(form_date_converter($_POST['tanggal_selesai'])),
		'transaksi_id'=>my_type_data_int($sk_id),
	);
	
	if( $id > 0 ){
		return my_update_record('pr_rapel' , 'rapel_id' , $id , $datas);
	}
	
	$id = my_insert_record('pr_rapel',$datas);
	
	//QUERY ADD RAPEL KARYAWAN
	$query = "SELECT x.karyawan_id,b.golongan_jabatan_id as sub_id, 
			a.kode_golongan_jabatan as label, 
			b.nominal 
			FROM golongan_jabatan a
			INNER JOIN kekaryawanan x ON x.golongan_jabatan_id = a.golongan_jabatan_id
			LEFT JOIN pr_sk_tunjangan_detail b ON a.golongan_jabatan_id=b.sub_id AND b.sk_id=".$sk_id."
			ORDER BY a.golongan_jabatan_id ASC";
	$result = my_query($query);
	
	//$data_golongan_id =
	
	while($ey = my_fetch_array($result)){
		 
		 detail_rapel_submit($karyawan_id , $rapel_id , $nominal );
	}
	
}

function get_data_nominal_golongan($sk_id    ){
	$sk = my_get_data_by_id('pr_sk_tunjangan','sk_id',$sk_id);
	 
	return $hasil_type_entry;
}


function detail_rapel_submit($karyawan_id , $rapel_id , $nominal){
	
	$datas = array(
		'nominal'=>my_type_data_int($nominal), 
		'rapel_id'=>my_type_data_int($rapel_id), 
		'karyawan_id'=>my_type_data_int($karyawan_id), 
	);
	
	if( $id > 0 ){
		return my_update_record('pr_rapel' , 'rapel_id' , $id , $datas);
	}
	
	return my_insert_record('pr_rapel',$datas);
}

function form_rapel($sk_id){
	 
	my_set_file_js(
		array(
			'components/system/jquery/autocomplete/jquery.autocomplete.js' ,
			'components/system/js/calendar/calendarDateInput.js'
		)
	);
	$view = form_header( "karyawan" , "karyawan"  );
	//$karyawan = loaddata_karyawan($karyawan_id);
	//$view .= form_field_display( $karyawan['nik']   , "NIK"   );
	//$view .= form_field_display( $karyawan['nama']   , "Nama karyawan"   );
	
	$fields = my_get_data_by_id('pr_sk_tunjangan','sk_id',$sk_id);
	$tanggal = array(
			'name'=>'tanggal',
			'value'=>(isset($_POST['tanggal'])? $_POST['tanggal'] : ( $fields ? date('d-m-Y' , strtotime($fields['tanggal'])) : date('d-m-Y')) ),
			'id'=>'tanggal', 
		);
	$form_tanggal  = form_calendar($tanggal );
	$view .= form_field_display( $form_tanggal   , "Tanggal efektif"  );
	 
	$query = "SELECT * FROM pr_komponen_gaji";
	$res = my_query($query);
	$komponens = array();
	while( $e = my_fetch_array($res) ){
		$komponens[$e['komponen_gaji_id']]=$e['nama_komponen_gaji'];
	}
	
	$komponen_gaji_id = array(
		'name'=>'komponen_gaji_id',
		'id'=>'komponen_gaji_id',
		'value'=>isset($_POST['komponen_gaji_id']) ? $_POST['komponen_gaji_id'] : $fields['komponen_gaji_id'],
	);
	$form_komponen  = form_dropdown($komponen_gaji_id, $komponens );
	$view .= form_field_display( $form_komponen   , "Komponen gaji"  );
 
	$view .= form_field_display( '&nbsp;'  , '&nbsp;'  );
	
	$tanggal_mulai = array(
			'name'=>'tanggal_mulai',
			'value'=>(isset($_POST['tanggal_mulai'])? $_POST['tanggal_mulai'] : ( $fields ? date('d-m-Y' , strtotime($fields['tanggal_mulai'])) : date('d-m-Y')) ),
			'id'=>'tanggal_mulai', 
		);
	$form_tanggal_mulai  = form_calendar($tanggal_mulai );
	$view .= form_field_display( $form_tanggal_mulai  , "Tanggal mulai"  );
	
	
	$tanggal_selesai = array(
			'name'=>'tanggal_selesai',
			'value'=>(isset($_POST['tanggal_selesai'])? $_POST['tanggal_selesai'] : ( $fields ? date('d-m-Y' , strtotime($fields['tanggal_selesai'])) : date('d-m-Y')) ),
			'id'=>'tanggal_selesai', 
		);
	$form_tanggal_s  = form_calendar($tanggal_selesai );
	$view .= form_field_display( $form_tanggal_s  , "Tanggal selesai"  );
	
	
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view; 
}

function submit_sk($id = 0){
	if($_POST){
		list( $ddtanggal, $mmtanggal,$yyyytanggal ) = explode("-" ,$_POST['tanggal'] );
		$ftanggal = $yyyytanggal.'-'.$mmtanggal.'-'.$ddtanggal;
		 
	}	
	$komponens=my_get_data_by_id('pr_komponen_gaji','komponen_gaji_id',$_POST['komponen_gaji_id']);
	$datas = array(
		'nomor_sk'=>my_type_data_str($_POST['nomor_sk']),
		'tanggal'=> my_type_data_str($ftanggal),	
		'komponen_gaji_id'=> my_type_data_int($_POST['komponen_gaji_id']),
		'model_entry'=> my_type_data_int($komponens['model_entry']),
	);
	 
	if($id > 0){
	
		return my_update_record('pr_sk_tunjangan','sk_id',$id,$datas);
	}
	$new_id = my_insert_record('pr_sk_tunjangan',$datas);
	return $new_id;
}

function delete_sk($id){
	$query= "DELETE FROM pr_sk_tunjangan WHERE sk_id = {$id}";
	$query_detail = "DELETE FROM pr_sk_tunjangan_detail WHERE sk_id = {$id}";
	my_query($query_detail);
	return my_query($query);

} 


/*
	=============================== END OF SK ==========================
*/	

function list_detail($sk_id){

	$fields = my_get_data_by_id('pr_sk_tunjangan','sk_id',$sk_id);
	$model_entry=$fields['model_entry'];
	$komponens = my_get_data_by_id('pr_komponen_gaji','komponen_gaji_id',$fields['komponen_gaji_id']);
	if($model_entry==4){ //profesi	
		$query = "SELECT a.profesi_detail_id as sub_id, 
			a.nama_profesi_detail as label, 
			b.nominal 
			FROM profesi_detail a
			LEFT JOIN pr_sk_tunjangan_detail b ON a.profesi_detail_id=b.sub_id AND b.sk_id=".$sk_id."
			WHERE a.profesi_id=".$komponens['profesi_id']." 
			ORDER BY a.profesi_detail_id ASC 
			";
		$sub='Profesi';	
	}else{
		$query = "SELECT a.golongan_jabatan_id as sub_id, 
			a.kode_golongan_jabatan as label, 
			b.nominal 
			FROM golongan_jabatan a
			LEFT JOIN pr_sk_tunjangan_detail b ON a.golongan_jabatan_id=b.sub_id AND b.sk_id=".$sk_id."
			ORDER BY a.golongan_jabatan_id ASC";
		$sub='Golongan Jabatan';		
	}
	$header = array(
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		$sub=>array('style'=>'text-align:left;border-bottom:2px solid;width:41%'),  
		'Nominal'=>array('style'=>'text-align:right;border-bottom:2px solid;width:22%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);
/*
	if($model_entry==4){ //profesi
		$query = "SELECT b.nama_profesi_detail as label, a.nominal FROM pr_sk_tunjangan_detail a 
			LEFT JOIN profesi_detail b ON b.profesi_detail_id=a.sub_id 
			WHERE a.sk_id=".$sk_id." 
			ORDER BY a.sub_id ASC 
			";
	}else{
		$query = "SELECT b.kode_golongan_jabatan as label, a.nominal FROM pr_sk_tunjangan_detail a 
			LEFT JOIN golongan_jabatan b ON b.golongan_jabatan_id=a.sub_id 
			WHERE a.sk_id=".$sk_id." 
			ORDER BY a.sub_id ASC 
			";
	}
*/	
	$result = my_query($query);
	$i=0;
	$row = array();
	while($ey = my_fetch_array($result) ){
		$i++;
		$editproperty = array(
			'href'=>'index.php?com=pr_sk_tunjangan&task=editrupiah&sk_id='.$sk_id.
			'&sub_id='.$ey['sub_id'].'&kg_id='. $fields['komponen_gaji_id'] ,
			'title'=>'Detail',
			'rel'=>'facebox'
		);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );

		$row[] = array(
			'#'=>position_text_align ($i, 'center'),    
			'sub'=>  $ey['label'] ,     
			'rupiah'=>  position_text_align( rp_format($ey['nominal']) , 'right'),
			'operasi'=> position_text_align( $edit_button , 'right'),
		);
	}
	$datas = table_rows($row);   
	$view = form_header( "pr_komponen_jabatan" , "pr_komponen_jabatan"  );
	$view .= form_field_display( $fields['nomor_sk']   , "SK No."   );
	$view .= form_field_display( $fields['tanggal']   , "Tanggal Efektif"   );
	$view .= form_field_display( $komponens['nama_komponen_gaji']   , "Komponen gaji"   );
	$view .= form_footer( );
	return  $view.'<br/>'. table_builder($header , $datas ,  5 , false );
}

function form_add_rupiah_create($sk_id,$kg_id,$sub_id){
	my_set_file_js(array(
		'components/system/jquery/jquery.formatCurrency-1.4.0.js', 
		)); 	
	$view = form_header( "..." , "..."  );
	$query = "SELECT * FROM pr_sk_tunjangan_detail 
			WHERE sk_id={$sk_id}
			AND komponen_gaji_id = {$kg_id}
			AND sub_id = {$sub_id}";
 	$fields = my_fetch_array( my_query($query));
	$komponen = my_get_data_by_id('pr_komponen_gaji','komponen_gaji_id',$kg_id);
	$sk_tunjangan = my_get_data_by_id('pr_sk_tunjangan','sk_id',$sk_id);
	$model_entry=$sk_tunjangan['model_entry'];
	$view .= form_field_display( $komponen['nama_komponen_gaji']   , "Komponen Gaji"   );
	if($model_entry==4){ //profesi
		$subs=my_get_data_by_id('profesi_detail','profesi_detail_id',$sub_id);
		$view .= form_field_display( $subs['nama_profesi_detail'] , "Profesi"   );
	}else{
		$subs=my_get_data_by_id('golongan_jabatan','golongan_jabatan_id',$sub_id);		
		$view .= form_field_display( $subs['kode_golongan_jabatan'] , "Golongan Jabatan"   );
	}

	$nominal = array(
		'name'=>'nominal',
		'value'=>( isset($_POST['nominal']) ? $_POST['nominal'] :rp_format( $fields['nominal'] )),
		'id'=>'nominal',
		'type'=>'text',
		'size'=>'25'
	);
	$form_field_nama = form_money($nominal); 
	$view .= form_field_display( $form_field_nama   , "Nominal"   );
	
	$submit = array(
		'value' =>  '  Update  ',
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
}

function submit_add_rupiah_create($sk_id,$kg_id,$sub_id){
	$rupiah = set_text_as_number($_POST['nominal']);
	$datas = array(
		'sk_id'	=>my_type_data_int($sk_id),
		'komponen_gaji_id'	=>my_type_data_int($kg_id),
		'sub_id'	=>my_type_data_int($sub_id),
		'nominal'	=>my_type_data_str($rupiah),
	);
	$query = "SELECT sk_tunjangan_detail_id FROM pr_sk_tunjangan_detail 
			WHERE sk_id={$sk_id}
			AND komponen_gaji_id = {$kg_id}
			AND sub_id = {$sub_id}";
	$reul = my_query($query);
	if(my_num_rows($reul) > 0){
		$data = my_fetch_array($reul);
		return my_update_record('pr_sk_tunjangan_detail' ,'sk_tunjangan_detail_id',$data['sk_tunjangan_detail_id'] , $datas);
	}
	return my_insert_record('pr_sk_tunjangan_detail' , $datas);
}